package com.example.springboot.config.healthindicator;




import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 自定义端点监控服务
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/7 15:05
 */
@Component
@Endpoint(id = "myEndpoint")
public class MyEndPoint {
    private String UP = "一切正常";
    private String DOWN = "有错误";

    @ReadOperation
    public JSONObject health_1() {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("up", UP);
        jsonObject.put("down", DOWN);
        return jsonObject;
    }

    @ReadOperation
    public JSONObject health_2(@Selector String name) {
        JSONObject jsonObject = new JSONObject();
        if ("up".equals(name)) {
            jsonObject.put("up", UP);
        } else if ("down".equals(name)) {
            jsonObject.put("down", DOWN);
        }
        return jsonObject;
    }

    @WriteOperation
    public void health_3(@Selector String name, @Nullable String value) {
        if (!StringUtils.isEmpty(value)) {
            if ("up".equals(name)) {
                UP = value;
            } else if ("down".equals(name)) {
                DOWN = value;
            }
        }
    }

}
